package com.ruoyi.web.controller.train;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.train.domain.TrainType;
import com.ruoyi.train.service.ITrainTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培训类型Controller
 *
 * @author ruoyi
 * @date 2022-09-01
 */
@RestController
@RequestMapping("/train/trainType")
public class TrainTypeController extends BaseController {
    @Autowired
    private ITrainTypeService trainTypeService;

    /**
     * 查询培训类型列表
     */
    @PreAuthorize("@ss.hasPermi('train:trainType:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainType trainType) {
        startPage();
        List<TrainType> list = trainTypeService.selectTrainTypeList(trainType);
        return getDataTable(list);
    }

    /**
     * 导出培训类型列表
     */
    @PreAuthorize("@ss.hasPermi('train:trainType:export')")
    @Log(title = "培训类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainType trainType) {
        List<TrainType> list = trainTypeService.selectTrainTypeList(trainType);
        ExcelUtil<TrainType> util = new ExcelUtil<TrainType>(TrainType.class);
        util.exportExcel(response, list, "培训类型数据");
    }

    /**
     * 获取培训类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('train:trainType:query')")
    @GetMapping(value = "/{trainTypeId}")
    public AjaxResult getInfo(@PathVariable("trainTypeId") Long trainTypeId) {
        return AjaxResult.success(trainTypeService.selectTrainTypeByTrainTypeId(trainTypeId));
    }

    /**
     * 新增培训类型
     */
    @PreAuthorize("@ss.hasPermi('train:trainType:add')")
    @Log(title = "培训类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainType trainType) {
        return toAjax(trainTypeService.insertTrainType(trainType));
    }

    /**
     * 修改培训类型
     */
    @PreAuthorize("@ss.hasPermi('train:trainType:edit')")
    @Log(title = "培训类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainType trainType) {
        return toAjax(trainTypeService.updateTrainType(trainType));
    }

    /**
     * 培训最低时间设置
     */
    @PreAuthorize("@ss.hasPermi('train:trainType:setMinTrainTime')")
    @Log(title = "培训最低时间设置", businessType = BusinessType.UPDATE)
    @PostMapping("/setMinTrainTime")
    public AjaxResult setMinTrainTime(@RequestBody Map<String, Object> map) {
        return toAjax(trainTypeService.setMinTrainTime(map.get("minTime").toString()));
    }

    /**
     * 培训最低时间查询
     */
    @GetMapping("selectMinTrainTime")
    public AjaxResult selectMinTrainTime() {
        return AjaxResult.success(trainTypeService.selectMinTrainTime());
    }


    /*
         删除培训类型
         */
    @PreAuthorize("@ss.hasPermi('train:trainType:remove')")
    @Log(title = "培训类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{trainTypeIds}")
    public AjaxResult remove(@PathVariable Long[] trainTypeIds) {
        return toAjax(trainTypeService.deleteTrainTypeByTrainTypeIds(trainTypeIds));
    }


}
